#include "StdAfx.h"
#include "libutilsbase.h"
#include "map"
#include "MessageHandler.h"

#include "umaths.h"
#include "ustringutils.h"
#include "IOrderService.h"
#include "RiskMgrService.h"
#include "TSOrderService.h"
USING_LIBSPACE

CMessageHandler::CMessageHandler(void)
{
}

CMessageHandler::~CMessageHandler(void)
{
}

bool CMessageHandler::ProcessMap( std::map<tstring,tstring>& themap )
{
	if(themap[_T("ope")] == _T("trade"))
	{
		tradeadapter::Order order;
		order.ContractCode = themap[_T("code")];
		order.Amount = UMaths::ToInt32( themap[_T("amount")] );
		order.Price = UMaths::ToDouble(themap[_T("price")]);
		order.OpeDir = (tradeadapter::BuySell)UMaths::ToInt32(themap[_T("bs")]);
		order.openclose = (tradeadapter::OpenClose ) UMaths::ToInt32(themap[_T("openclose")]);
		//order.openclose

		

		if( CRiskMgrServiceHolder::Instance().ControlTrade(_T("directorder"),order) )
		{
			if(order.ContractCode.substr(0,2) == _T("IF"))
			{
				tradeadapter::IOrderService*  fs  = TSOrderServiceHoder::Instance().GetFutureService();
				if(fs != NULL)
				{
					fs->EntrustOrder(_T("directorder"),order);
				}
			}
			else
			{			
				TSOrderServiceHoder::Instance().GetStockOrderService()->EntrustOrder(_T("directorder"),order)	;
			}
		}

	}
	return true;
}
